package com.anyou.mall.order.controller;

import cn.hutool.core.collection.CollUtil;
import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.core.domain.entity.SysUser;
import com.anyou.common.core.page.TableDataInfo;
import com.anyou.common.enums.BusinessType;
import com.anyou.common.exception.ServiceException;
import com.anyou.common.utils.StringUtils;

import com.anyou.mall.order.domain.OrdOrder;
import com.anyou.mall.order.param.DeliveryParam;
import com.anyou.mall.order.param.DeliveryParam2;
import com.anyou.mall.order.param.DriverListParam;
import com.anyou.mall.order.param.InboundParam;
import com.anyou.mall.order.service.IOrdOrderService;
import com.anyou.mall.order.vo.DriverListVo;
import com.anyou.mall.order.vo.DriverOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 配送列表Controller
 *
 * @author ruoyi
 * @date 2022-04-02
 */
@Api(tags = "订单配送列表")
@RestController
@RequestMapping("/deliveryorder/ord")
public class DeliveryOrderController extends BaseController
{
    @Autowired
    private IOrdOrderService ordOrderService;






    /**
     * 查询配送列表列表
     */
    @ApiOperation("配送订单列表")
    @PreAuthorize("@ss.hasPermi('deliveryorder:ord:list')")
    @GetMapping("/list")
    public TableDataInfo<List<DriverOrder>> list(DriverOrder ordOrder)
    {
//        SysUser user=ordOrderService.selectUserById(getUserId());
        startPage();
        List<DriverOrder> list=ordOrderService.deliverOrderList(ordOrder);
        return getDataTable(list);
    }


    /**
     * 详情
     */
    @ApiOperation("配送订单详情")
    @PreAuthorize("@ss.hasPermi('deliveryorder:ord:query')")
    @GetMapping(value = "/{orderId}")
    public ApiResult<DriverOrder> getInfo(@PathVariable("orderId") Long orderId)
    {
        DriverOrder driverOrder=ordOrderService.deliverOrderInfo(orderId);
        return ApiResult.success(driverOrder);
    }



    /**
     * 分配配送员
     */
    @ApiOperation("分配/改派司机")
    @PreAuthorize("@ss.hasPermi('deliveryorder:ord:distrDeliveryDriver')")
    @Log(title = "分配配送员", businessType = BusinessType.UPDATE)
    @PutMapping("/distrDeliveryDriver")
    public ApiResult distrDeliveryDriver(@Valid @RequestBody DeliveryParam parm)
    {
        //判断参数是否有传
        pdParam(parm);
        ordOrderService.distrDeliveryDriver(parm);

        return ApiResult.success();
    }
    /**
     * 查询司机列表
     */
    @ApiOperation("司机列表")
    @PreAuthorize("@ss.hasPermi('deliveryorder:ord:driverList')")
    @PutMapping("/driverList")
    public TableDataInfo<List<DriverListVo>> driverList(@Valid @RequestBody DriverListParam param)
    {
        startPage();
        List<DriverListVo> list=ordOrderService.driverList(param);
        return getDataTable(list);
    }

    /**
     * 判断参数是否有传
     * @param parm
     */
    private void pdParam(DeliveryParam parm) {
        if(parm.getType()==1){
            //1:分配司机
            if(parm.getMothed()==null||parm.getIsUrgentDelivery()==null
                || CollUtil.isEmpty(parm.getOrderIds())){
                throw new ServiceException("分配司机时,分配方式,订单id,是否加急等参数不能为空！");
            }
            if(parm.getMothed()==2&&parm.getDriverId()==null){
                throw new ServiceException("分配司机时，选指定司机时，司机id参数不能为空！");
            }
        }
        if(parm.getType()==2){
            //2:改派司机
            if(parm.getDriverId()==null
                    || CollUtil.isEmpty(parm.getOrderIds())){
                throw new ServiceException("改派司机时，司机id,订单id等参数不能为空！");
            }

        }
    }


}
